package com.cczhr.bluetoothlogcat;

import android.util.Log;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class MyThread implements Runnable {
    private static int i;
    private static final Lock lock = new ReentrantLock();
    private static AtomicInteger i1 = new AtomicInteger(0);

    @Override
    public void run() {

        while (true){
            execute3();
            try {
                Thread.sleep(500);

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void  execute1(){
        synchronized (MyThread.class){
                Log.e("线程池" + Thread.currentThread().getId(), "run: " + (++i));

        }
    }
      public synchronized static void execute(){
        Log.e("线程池"+Thread.currentThread().getId(), "run: "+(++i));

    }
    public void  execute2(){
        lock.lock();
        try {
            Log.e("线程池" + Thread.currentThread().getId(), "run: " + (++i));

        }finally {
            lock.unlock();
        }
    }
    public void  execute3(){
        Log.e("线程池" + Thread.currentThread().getId(), "run: " + i1.incrementAndGet());
    }
}
